package com.lagou.module4hw.hw4;

import java.io.*;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {


        Socket socket = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            //1.创建Socket类型的对象并提供服务器的IP地址和端口号
            socket = new Socket("127.0.0.1",8888);

            //2.给服务器发送对象
            UserMessage tum = new UserMessage("check", new User("admin", "123456"));
            OutputStream outputStream = socket.getOutputStream();
            oos = new ObjectOutputStream(outputStream);
            oos.writeObject(tum);

            //3.接受服务器的校验信息
            InputStream inputStream = socket.getInputStream();
            ois = new ObjectInputStream(inputStream);
            tum = (UserMessage) ois.readObject();
            System.out.println("服务器返回的信息是: " + tum);


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
               if ( ois != null ) ois.close();
               if ( oos != null ) oos.close();
               if ( socket != null ) socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
